package com.controller;

import com.model.Clazz;
import com.service.ClazzService;
import com.utils.PageParam;
import com.utils.Pager;
import com.utils.R;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RequestMapping("clazz")
@RestController
public class ClazzController {

    @Resource
    private ClazzService clazzService;

    @RequestMapping(value = "/getClazzPager")
    public R getClazzPager(String yearCode, String clazzName, Integer pageIndex, Integer pageSize) {
        Pager<Clazz> pager = clazzService.getClazzPage(yearCode, clazzName, new PageParam(pageIndex, pageSize));
        return R.ok().put("data", pager);
    }

    /**
     * 后端保存
     */
    @RequestMapping("/saveClazz")
    public R saveClazz(Integer id, String clazzCode, String clazzName, String yearCode, String teacherId){
        Clazz clazz = new Clazz();
        clazz.setId(id);
        clazz.setClazzCode(clazzCode);
        clazz.setClazzName(clazzName);
        clazz.setYearCode(yearCode);
        clazz.setTeacherId(teacherId);
        if (id != null) {
            clazzService.updateClazz(clazz);
        } else {
            clazzService.saveClazz(clazz);
        }
        return R.ok();
    }

    @RequestMapping("/getAllClazz")
    public R getAllClazz(){
        List<Clazz> clazzes = clazzService.getAllClazz();
        return R.ok().put("data", clazzes);
    }

    /**
     * 删除
     */
    @RequestMapping("/delClazz")
    public R delClazz(String ids){
        String[] strArr = ids.split(",");
        List<Long> idList = new ArrayList<>();
        for (String str : strArr) {
            idList.add(Long.valueOf(str));
        }
        clazzService.delClazz(idList);
        return R.ok();
    }

}
